package com.hujiang.basic.search.common.component;

import java.util.concurrent.ExecutionException;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.hujiang.basic.search.common.converter.Converter;

public class ConverterFinder {

   private static LoadingCache<String, Class<?>> classCache = CacheBuilder.newBuilder()
            .build(new CacheLoader<String, Class<?>>() {
                @Override
                public Class<?> load(String key) throws Exception {
                    return Class.forName(key);
                }
            });

    @SuppressWarnings("rawtypes")
    public static Converter find(String className, ApplicationContext ctx)
            throws BeansException, ExecutionException {
        return (Converter) ctx.getBean(classCache.get(className));
    }
}
